package com.gongpingjialoan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.cc.Const;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.imageload.ImageLoad;
import com.gongpingjialoan.utility.Utils;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/16.
 */
public class NewHotBrandAdapter  extends BaseAdapter {

    private Context mContext;

    private LayoutInflater mInflater;

    private List<Map<String, String>> data;


    public NewHotBrandAdapter(Context c, List<Map<String, String>> data)
    {
        this.mContext = c;
        this.data = data;
        mInflater = LayoutInflater.from(mContext);
    }


    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {

        try
        {
            return data.get(position);
        }
        catch (Exception e)
        {
            Utils.LogE("异常" + e);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Map<String, String> map = data.get(position);
        ViewHolder holder;
        if (convertView == null)
        {
            convertView = mInflater.inflate(R.layout.hot_brand_item, null);
            holder = new ViewHolder();
            holder.info_txt = (TextView)convertView.findViewById(R.id.info_txt);
            holder.brandLogo = (ImageView) convertView.findViewById(R.id.brandLogo);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder)convertView.getTag();
        }
        try
        {

            String filename = map.get("logo_img");
            String logo_url =
                    Const.IMG_DOMAIN
                            + GPJApplication.getInstance().getApiUrlFromMeta("brand_model_logo_img")
                            + filename + "?imageView2/0/w/100/h/100";

            ImageLoad.LoadImage(holder.brandLogo, logo_url, R.drawable.brandnull, R.drawable.brandnull,mContext);
            // Glide.with(BrandFragment.this).load(logo_url).placeholder(R.drawable.brandnull).error(R.drawable.brandnull).into(holder.brandLogo);
            holder.info_txt.setText(map.get("name"));
        }
        catch (Exception e)
        {
            Utils.LogE("热门品牌ITEM异常：" + e.getMessage());
        }
        return convertView;
    }

    private static class ViewHolder
    {
        public TextView info_txt;

        public ImageView brandLogo;
    }
}
